package com.api.gbhg.zengping.web;

import com.api.gbhg.liuwenbo.api.ProjectDetailsApi;
import com.api.gbhg.liuwenbo.dao.ProjectDetailsUtil;
import com.api.gbhg.liuwenbo.utils.Utils;
import com.api.gbhg.liwenjun.VersionFlowUtil;
import com.api.gbhg.zengping.common.CommonDao;
import com.api.gbhg.zengping.entity.Info;
import weaver.conn.RecordSet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: gbhg-oa
 * @description: this is a class
 * @author: Mr.zeng
 * @create: 2021-02-05 18:02
 **/
public class TestGetInfo {
    ProjectDetailsApi detailsApi=new ProjectDetailsApi();


    public Info[] DetailsData(Map<String,Object> param){

        if (!param.containsKey("requestId")){
            Map<String,Object> map  = new HashMap<>();
            map.put("retcode","-1");
            map.put("retmsg","请检查上送参数！为必填项");
            return null;
        }

        Info[] infos = getSysDataById(param.get("requestId").toString());
        return infos;
    }
    /**
     * 根据requestId获取流程数据详情
     * @param requestId
     * @return
     */
    public Info[] getSysDataById(String requestId){
        RecordSet rs = new RecordSet();

        String lcid = ProjectDetailsUtil.getFlowIdByRequestId(requestId);
        String title = ProjectDetailsUtil.getFlowIdByRequestName(requestId);
        String flowTableName = VersionFlowUtil.getFlowTableName(lcid);

        //获取主表数据
        Map<String, Object> tabData = new CommonDao().getMainTabData(flowTableName, requestId);

        int mainTableId = VersionFlowUtil.getFlowTableLableId(lcid);
        //获取明细表名称
        List<String> flowDetailTableNames = VersionFlowUtil.getDetailTables(mainTableId);

        Info[] info = new Info[flowDetailTableNames.size()+1];
        Map<String,Object> map = new HashMap<>();
        info[0] = new Info();
        map.putAll(tabData);
        map.put("title",title);
        info[0].setTitle("tab");
        info[0].setTable(map);

        rs.execute("select id from "+flowTableName+" where requestId="+requestId);
        rs.next();
        int mainId= rs.getInt("id");

        //获取明细数据
        for (int i = 0; i < flowDetailTableNames.size(); i++) {
            // 明细表名
            String detailName = flowDetailTableNames.get(i).substring(flowDetailTableNames.get(i).length() - 3);
            if(detailName.equals("dt1")){
                Map<String, Object>[] maps = detailsApi.getdtTable(flowDetailTableNames.get(i), mainId);
                if (maps.length>0){
                    info[i+1] = new Info();
                    info[i+1].setData(Utils.removeExcessLen(maps));
                    info[i+1].setTitle(detailName);
                }
            }
            if(detailName.equals("dt2")){
                Map<String, Object>[] maps = detailsApi.getdtTable(flowDetailTableNames.get(i), mainId);
                if (maps.length>0){
                    info[i+1] = new Info();
                    info[i+1].setData1(Utils.removeExcessLen(maps));
                    info[i+1].setTitle(detailName);
                }
            }
            if(detailName.equals("dt3")){
                Map<String, Object>[] maps = detailsApi.getdtTable(flowDetailTableNames.get(i), mainId);
                if (maps.length>0){
                    info[i+1] = new Info();
                    info[i+1].setData2(Utils.removeExcessLen(maps));
                    info[i+1].setTitle(detailName);
                }
            }
            if(detailName.equals("dt4")){
                Map<String, Object>[] maps = detailsApi.getdtTable(flowDetailTableNames.get(i), mainId);
                if (maps.length>0){
                    info[i+1] = new Info();
                    info[i+1].setData3(Utils.removeExcessLen(maps));
                    info[i+1].setTitle(detailName);
                }
            }

        }
        return removeExcessLenInfo(info);
    }
    /**
     * 去除info数组多余长度
     * @return
     */
    public Info[] removeExcessLenInfo(Info[] infos){
        List<Info> list = new ArrayList<>();
        for (Info info :infos) {
            if(info != null){
                list.add(info);
            }
        }
        Info[] newInfos = new Info[list.size()];
        for (int j = 0; j < newInfos.length; j++) {
            newInfos[j] = new Info();
            // 主表
            if(list.get(j).getTable()!=null){
                newInfos[j].setTable(list.get(j).getTable());
            }
            // 明细表
            if(list.get(j).getData() != null) {
                newInfos[j].setData(list.get(j).getData());
            }
            if(list.get(j).getData1()!=null){
                newInfos[j].setData1(list.get(j).getData1());
            }
            if(list.get(j).getData2()!=null){
                newInfos[j].setData2(list.get(j).getData2());
            }
            if(list.get(j).getData3()!=null){
                newInfos[j].setData3(list.get(j).getData3());
            }
            if(list.get(j).getData4()!=null){
                newInfos[j].setData4(list.get(j).getData4());
            }

            newInfos[j].setTitle(list.get(j).getTitle());
        }
        return newInfos;
    }


}
